home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15859 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: morgan.cnu.edu!dlabell
  2. From: dlabell@pcs.cnu.edu (Daniel LaBell)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ meta-problem
  5. Date: 08 Apr 1996 15:56:24 GMT
  6. Organization: A poorly-installed InterNetNews site
  7. Message-ID: <DLABELL.96Apr8115624@resolute.pcs.cnu.edu>
  8. References: <4jmtlg$smm@baskerville.CS.Arizona.EDU>
  9. NNTP-Posting-Host: resolute.pcs.cnu.edu
  10. In-reply-to: kdb@CS.Arizona.EDU's message of 31 Mar 1996 14:32:32 -0700
  11.  
  12. In article <4jmtlg$smm@baskerville.CS.Arizona.EDU> kdb@CS.Arizona.EDU (Koen De Bosschere) writes:
  13.  
  14. ) From: kdb@CS.Arizona.EDU (Koen De Bosschere)
  15. ) Newsgroups: comp.lang.c++
  16. ) Date: 31 Mar 1996 14:32:32 -0700
  17. ) In order to prevent to write an iterator for every operation 
  18. ) I want to perform on a list, I tried to write a kind of 
  19. ) meta-iterator that would apply a particular method to 
  20. ) all listnodes. For some reason, I cannot execute a 
  21. ) function variable on an object. Does anyone has an
  22. ) idea how I can solve this problem?
  23. [...]
  24. ) void iterate(testclass *root, void (testclass::*f)())
  25. ) {
  26. )   testclass *p = root;
  27. )   while (p) { 
  28. )     p->f();      //  <--- here the compiler generates an error (see below)
  29. )     p = p->getnext();
  30. )   }
  31. ) }
  32. [...]
  33.  
  34. I have just done something somewhat like this myself, but I can't fully 
  35. see what exactly what you're trying to do with 'p->f();'  but I think the
  36. concept is similiar.
  37.  
  38. (here's the part of the code that I think is relevant) 
  39.  
  40. void binSrchTree::inorder( void ( *proccessFunc) (class record a))
  41. {
  42.   if ( root == NULL )
  43.     return ;
  44.   inorder ( root, proccessFunc );
  45. }
  46.  
  47. void binSrchTree::inorder( binSrchTreeNode* n, 
  48.                           void (*proccessFunc) (class record a))
  49. {
  50.   
  51.   if ( n->leftChild != NULL )
  52.     inorder( n->leftChild, proccessFunc );
  53.   (*proccessFunc) (n->data);
  54.   if ( n->rightChild != NULL )
  55.     inorder( n->rightChild, proccessFunc );
  56. }
  57.  
  58. void PrintRecAndCompute ( record a )
  59. {
  60.   Count++;
  61.   AddToAverages( a );
  62.   cout.flags( ios::left );
  63.   cout << setw(25) << a.name << setprecision(4) << a.grades[0] 
  64.     << TAB  << setprecision(4) << a.grades[1]
  65.       << TAB << setprecision(4) << a.grades[2]
  66.      << TAB << setprecision(4)  << a.grades[3] << endl;
  67. }
  68.  
  69. I'd call it with a line like this:
  70.     Tree1.inorder(PrintRecAndCompute);
  71. --
  72. Daniel LaBell
  73.